package Service;

import Entity.*;
import UI.ChangeUI;
import UI.Exam;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class ContentContext {
    /*
    所有的业务的逻辑判断
        1.生成试卷
        2.开始考试
        3.上一题
        4.下一题
        5.交卷
        6.倒计时

     控制器:所有的逻辑判断,需要(用户)数据和数据库中的数据进行对比
            最后将结果返回给前端
 */
    private entityContext entityContext;
    private ChangeUI changeUI;
    private List<QuestionInfo> paper = new ArrayList<QuestionInfo>();
    private User user;

    public void setChangeUI(ChangeUI changeUI) {
        this.changeUI = changeUI;
    }

    public void setEntityContext(Entity.entityContext entityContext) {
        this.entityContext = entityContext;
    }

    public User correct(int id) throws idOrPwdException {
        user = entityContext.getUserid(id);
        if (user==null){
            throw new idOrPwdException("账号或密码错误");
        }
        return user;
    }
    public String getRules() {
        String rules = entityContext.getRules();
        return rules;
    }

    private boolean finished = false;
    private boolean started = false;
    public ExamInfo start() {
        if (finished)
            throw new RuntimeException("考试已经结束了!");
        if (started)
            throw new RuntimeException("考试已经开始了!");
        // 创建考卷
        createPaper();
        // 组织考试信息
        ExamInfo info = new ExamInfo();
        info.setQuestionCount(paper.size());
//        info.setTimeLimit(entityContext.getTimeLimit());
//        info.setTitle(entityContext.getTitle());
        info.setUser(user);// 当前系统登陆用户
        started = true;
        return info;
    }
    public QuestionInfo getQuestion(int index) {
        return paper.get(index);
    }

    private void createPaper() {
        Random r = new Random();
        int index = 0;
        for (int level = Question.LEVEL1; level <= Question.LEVEL10; level++) {
            List<Question> list = entityContext.findQuestions(level);
            // 从list中取出(remove)一道题
            Question q1 = list.remove(r.nextInt(list.size()));
            Question q2 = list.remove(r.nextInt(list.size()));
            paper.add(new QuestionInfo(index++, q1));
            paper.add(new QuestionInfo(index++, q2));
        }
    }

    public void saveUserAnswers(int index, List<Integer> userAnswers) {
        QuestionInfo q = paper.get(index);
        q.getUserAnswers().clear();
        q.getUserAnswers().addAll(userAnswers);
    }
    private int score;
    public int examOver() {
        if (finished)
            throw new RuntimeException("考试已经结束!");
        for (QuestionInfo q : paper) {
            Question question = q.getQuestion();
            List<Integer> userAnswers = q.getUserAnswers();
            if (userAnswers.equals(question.getAnswers())) {
                score += question.getScore();
            }
        }
        finished = true;
        return score;
    }
//    public Map getPaper() {
//        System.out.println(entityContext.getQuestions().get(1));
//        for (int i = 0; i < 10; i++) {
//            int size = entityContext.getQuestions().get(i+1).size();
//            int ran= (int) (Math.random()*size);
//            int ran2= (int) (Math.random()*size);
//            while (ran==ran2){
//                ran=(int) (Math.random()*size)+1;
//            }
//            Question question = entityContext.getQuestions().get(i+1).get(ran);
//            Question question2 = entityContext.getQuestions().get(i+1).get(ran2);
//            entityContext.getPaper().put(i+1,question);
//            entityContext.getPaper().put(i+2,question2);
//        }
//        System.out.println(entityContext.getPaper());
//        return entityContext.getPaper();
//    }
}
